package cn.aran.org.controller;

import cn.aran.basic.annotation.PreAuthorize;
import cn.aran.basic.exception.BusinessException;
import cn.aran.basic.utils.AjaxResult;
import cn.aran.basic.utils.ExcelUtil;
import cn.aran.basic.utils.PageList;
import cn.aran.org.domain.Shop;
import cn.aran.org.domain.ShopAuditLog;
import cn.aran.org.query.ShopQuery;
import cn.aran.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;

    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "保存店铺", sn = "shop:save")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除店铺", sn = "shop:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.loadById(id);
    }

    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.loadAll();
    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name = "列表店铺", sn = "shop:list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryData(query);
    }

    /**
    * @Title: settlement
    * @Description: 店铺入驻
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/1 15:04
    * @Parameters: [shop]
    * @Return cn.aran.basic.utils.AjaxResult
    */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }
    }

    /**
    * @Title: pass
    * @Description: 店铺审核通过
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/2 20:37
    * @Parameters: [shopAuditLog]
    * @Return cn.aran.basic.utils.AjaxResult
    */
    @PostMapping("/audit/pass")
    @PreAuthorize(name = "店铺审核通过", sn = "shop:pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }
    }

    /**
    * @Title: activation
    * @Description: 店铺激活
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/3 15:26
    * @Parameters: [id]
    * @Return cn.aran.basic.utils.AjaxResult
    */
    @PutMapping("/active/{id}")
    public AjaxResult activation(@PathVariable("id")Long id){
        try {
            shopService.active(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * @Title: reject
    * @Description: 店铺审核驳回
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/2 20:38
    * @Parameters: [shopAuditLog]
    * @Return cn.aran.basic.utils.AjaxResult
    */
    @PostMapping("/audit/reject")
    @PreAuthorize(name = "店铺审核驳回", sn = "shop:reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }
    }

    /**
     * @Title: reject
     * @Description: 店铺审核拒绝
     * @Author: Aran
     * @Version: 1.0
     * @Date:  2022/7/2 20:38
     * @Parameters: [shopAuditLog]
     * @Return cn.aran.basic.utils.AjaxResult
     */
    @PostMapping("/audit/refuse")
    @PreAuthorize(name = "店铺审核拒绝", sn = "shop:refuse")
    public AjaxResult refuse(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.refuse(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }
    }

    /**
    * @Title: exportExcel
    * @Description: 导出店铺信息
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/3 9:43
    * @Parameters: [response]
    * @Return void
    */
    @GetMapping("/export/excel")
    @PreAuthorize(name = "导出店铺", sn = "shop:export")
    public void exportExcel(HttpServletResponse response)
    {
        List<Shop> shops = shopService.loadAll();
        ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "店铺信息表.xlsx", response);
    }

    /**
    * @Title: importExcel
    * @Description: 导入店铺信息
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/3 10:26
    * @Parameters: [file]
    * @Return void
    */
    @PostMapping("/import/excel")
    @PreAuthorize(name = "导入店铺", sn = "shop:import")
    public void importExcel(@RequestPart(value = "file", required = true) MultipartFile file){
        // 1.使用EasyPoi帮你解析Excel，解析成Java对象
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        // 2.做批量添加，自行实现
        shopService.batchSave(shops);
        shops.forEach(System.out::println);

    }

    /**
    * @Title: echarts
    * @Description: 店铺报表数据
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/3 10:29
    * @Parameters: []
    * @Return Map<String,Object>
    */
    @GetMapping("/echarts")
    public Map<String, Object> echarts(){
        return shopService.loadEcharts();
    }



}
